<!-- 文件名: app/templates/stats.html -->
{% extends "base.html" %}

{% block title %}统计信息 - 社交媒体监控{% endblock %}

{% block page_title %}统计信息{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-3 mb-4">
        <div class="card text-white bg-primary">
            <div class="card-body">
                <h5 class="card-title">总用户数</h5>
                <p class="card-text display-4">{{ stats.total_users }}</p>
            </div>
        </div>
    </div>
    <div class="col-md-3 mb-4">
        <div class="card text-white bg-secondary">
            <div class="card-body">
                <h5 class="card-title">总视频数</h5>
                <p class="card-text display-4">{{ stats.total_videos }}</p>
            </div>
        </div>
    </div>
    <div class="col-md-3 mb-4">
        <div class="card text-white bg-success">
            <div class="card-body">
                <h5 class="card-title">已下载视频</h5>
                <p class="card-text display-4">{{ stats.downloaded_videos }}</p>
            </div>
        </div>
    </div>
    <div class="col-md-3 mb-4">
        <div class="card text-white bg-danger">
            <div class="card-body">
                <h5 class="card-title">下载失败视频</h5>
                <p class="card-text display-4">{{ stats.failed_videos }}</p>
            </div>
        </div>
    </div>
</div>

<h3>平台统计</h3>
<div class="row">
    <div class="col-md-4 mb-4">
        <div class="card">
            <div class="card-header platform-tiktok">
                TikTok
            </div>
            <div class="card-body">
                <p>用户数: {{ stats.platforms.tiktok.user_count }}</p>
                <p>视频数: {{ stats.platforms.tiktok.video_count }}</p>
                <p>已下载: {{ stats.platforms.tiktok.downloaded_count }}</p>
                <p>下载失败: {{ stats.platforms.tiktok.failed_count }}</p>
            </div>
        </div>
    </div>
    <div class="col-md-4 mb-4">
        <div class="card">
            <div class="card-header platform-douyin">
                抖音
            </div>
            <div class="card-body">
                <p>用户数: {{ stats.platforms.douyin.user_count }}</p>
                <p>视频数: {{ stats.platforms.douyin.video_count }}</p>
                <p>已下载: {{ stats.platforms.douyin.downloaded_count }}</p>
                <p>下载失败: {{ stats.platforms.douyin.failed_count }}</p>
            </div>
        </div>
    </div>
    <div class="col-md-4 mb-4">
        <div class="card">
            <div class="card-header platform-instagram">
                Instagram
            </div>
            <div class="card-body">
                <p>用户数: {{ stats.platforms.instagram.user_count }}</p>
                <p>视频数: {{ stats.platforms.instagram.video_count }}</p>
                <p>已下载: {{ stats.platforms.instagram.downloaded_count }}</p>
                <p>下载失败: {{ stats.platforms.instagram.failed_count }}</p>
            </div>
        </div>
    </div>
    <div class="col-md-4 mb-4">
        <div class="card">
            <div class="card-header platform-kuaishou">
                快手
            </div>
            <div class="card-body">
                <p>用户数: {{ stats.platforms.kuaishou.user_count }}</p>
                <p>视频数: {{ stats.platforms.kuaishou.video_count }}</p>
                <p>已下载: {{ stats.platforms.kuaishou.downloaded_count }}</p>
                <p>下载失败: {{ stats.platforms.kuaishou.failed_count }}</p>
            </div>
        </div>
    </div>
</div>

<div class="card mb-4">
    <div class="card-header">
        <h5>监控状态</h5>
    </div>
    <div class="card-body">
        <div class="row">
            <div class="col-md-4 mb-4">
                <div class="card">
                    <div class="card-header platform-tiktok text-white">
                        TikTok
                    </div>
                    <div class="card-body">
                        {% set tiktok_enabled = settings|selectattr('key', 'equalto', 'tiktok_monitoring_enabled')|map(attribute='value')|first|default('true') %}
                        {% set tiktok_interval = settings|selectattr('key', 'equalto', 'tiktok_check_interval')|map(attribute='value')|first|default('3600') %}
                        <p class="mb-1">
                            状态:
                            {% if tiktok_enabled == 'true' %}
                            <span class="badge bg-success">已启用</span>
                            {% else %}
                            <span class="badge bg-danger">已禁用</span>
                            {% endif %}
                        </p>
                        <p class="mb-1">检查间隔: {{ (tiktok_interval|int / 60)|round }} 分钟</p>
                        <p class="mb-0">用户数: {{ stats.platforms.tiktok.user_count }}</p>
                    </div>
                </div>
            </div>

            <div class="col-md-4 mb-4">
                <div class="card">
                    <div class="card-header platform-douyin text-white">
                        抖音
                    </div>
                    <div class="card-body">
                        {% set douyin_enabled = settings|selectattr('key', 'equalto', 'douyin_monitoring_enabled')|map(attribute='value')|first|default('true') %}
                        {% set douyin_interval = settings|selectattr('key', 'equalto', 'douyin_check_interval')|map(attribute='value')|first|default('7200') %}
                        <p class="mb-1">
                            状态:
                            {% if douyin_enabled == 'true' %}
                            <span class="badge bg-success">已启用</span>
                            {% else %}
                            <span class="badge bg-danger">已禁用</span>
                            {% endif %}
                        </p>
                        <p class="mb-1">检查间隔: {{ (douyin_interval|int / 60)|round }} 分钟</p>
                        <p class="mb-0">用户数: {{ stats.platforms.douyin.user_count }}</p>
                    </div>
                </div>
            </div>

            <div class="col-md-4 mb-4">
                <div class="card">
                    <div class="card-header platform-instagram text-dark">
                        Instagram
                    </div>
                    <div class="card-body">
                        {% set instagram_enabled = settings|selectattr('key', 'equalto', 'instagram_monitoring_enabled')|map(attribute='value')|first|default('true') %}
                        {% set instagram_interval = settings|selectattr('key', 'equalto', 'instagram_check_interval')|map(attribute='value')|first|default('10800') %}
                        <p class="mb-1">
                            状态:
                            {% if instagram_enabled == 'true' %}
                            <span class="badge bg-success">已启用</span>
                            {% else %}
                            <span class="badge bg-danger">已禁用</span>
                            {% endif %}
                        </p>
                        <p class="mb-1">检查间隔: {{ (instagram_interval|int / 60)|round }} 分钟</p>
                        <p class="mb-0">用户数: {{ stats.platforms.instagram.user_count }}</p>
                    </div>
                </div>
            </div>

            <div class="col-md-4 mb-4">
                <div class="card">
                    <div class="card-header platform-kuaishou text-dark">
                        快手
                    </div>
                    <div class="card-body">
                        {% set kuaishou_enabled = settings|selectattr('key', 'equalto', 'kuaishou_monitoring_enabled')|map(attribute='value')|first|default('true') %}
                        {% set kuaishou_interval = settings|selectattr('key', 'equalto', 'kuaishou_check_interval')|map(attribute='value')|first|default('14400') %}
                        <p class="mb-1">
                            状态:
                            {% if kuaishou_enabled == 'true' %}
                            <span class="badge bg-success">已启用</span>
                            {% else %}
                            <span class="badge bg-danger">已禁用</span>
                            {% endif %}
                        </p>
                        <p class="mb-1">检查间隔: {{ (kuaishou_interval|int / 60)|round }} 分钟</p>
                        <p class="mb-0">用户数: {{ stats.platforms.kuaishou.user_count }}</p>
                    </div>
                </div>
            </div>

            <div class="col-md-4 mb-4">
                <div class="card">
                    <div class="card-header platform-retry text-white">
                        下载重试
                    </div>
                    <div class="card-body">
                        {% set retry_enabled = settings|selectattr('key', 'equalto', 'retry_task_enabled')|map(attribute='value')|first|default('true') %}
                        {% set retry_interval = settings|selectattr('key', 'equalto', 'failed_download_retry_interval')|map(attribute='value')|first|default('3600') %}
                        <p class="mb-1">
                            状态:
                            {% if retry_enabled == 'true' %}
                            <span class="badge bg-success">已启用</span>
                            {% else %}
                            <span class="badge bg-danger">已禁用</span>
                            {% endif %}
                        </p>
                        <p class="mb-1">检查间隔: {{ (retry_interval|int / 60)|round }} 分钟</p>
                    </div>
                </div>
            </div>

            <div class="col-md-4 mb-4">
                <div class="card">
                    <div class="card-header platform-hot text-white">
                        拉取热门视频
                    </div>
                    <div class="card-body">
                        {% set hot_enabled = settings|selectattr('key', 'equalto', 'hot_videos_enabled')|map(attribute='value')|first|default('true') %}
                        {% set hot_interval = settings|selectattr('key', 'equalto', 'hot_videos_check_interval')|map(attribute='value')|first|default('3600') %}
                        <p class="mb-1">
                            状态:
                            {% if hot_enabled == 'true' %}
                            <span class="badge bg-success">已启用</span>
                            {% else %}
                            <span class="badge bg-danger">已禁用</span>
                            {% endif %}
                        </p>
                        <p class="mb-1">检查间隔: {{ (hot_interval|int / 60)|round }} 分钟</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 定时任务状态 -->
<div class="card mb-4">
    <div class="card-header">
        <h5>定时任务状态</h5>
        <button class="btn btn-sm btn-outline-primary" onclick="location.reload()">
            <i class="bi bi-arrow-clockwise"></i> 刷新
        </button>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>任务名称</th>
                        <th>状态</th>
                        <th>上次运行时间</th>
                        <th>下次运行时间</th>
                        <th>最近结果</th>
                    </tr>
                </thead>
                <tbody>
                    {% for task in tasks %}
                    <tr>
                        <td>{{ task.task_name }}</td>
                        <td>
                            {% if task.status == 'idle' %}
                            <span class="badge bg-secondary">空闲</span>
                            {% elif task.status == 'running' %}
                            <span class="badge bg-primary">运行中</span>
                            {% elif task.status == 'completed' %}
                            <span class="badge bg-success">已完成</span>
                            {% elif task.status == 'failed' %}
                            <span class="badge bg-danger">失败</span>
                            {% else %}
                            <span class="badge bg-warning">{{ task.status }}</span>
                            {% endif %}
                        </td>
                        <td>{{ task.last_run_at }}</td>
                        <td>{{ task.next_run_at }}</td>
                        <td>{{ task.last_result }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}
{% block scripts %}
<script>
    // 每30秒自动刷新页面
    setTimeout(function() {
        location.reload();
    }, 30000);
</script>
{% endblock %}